package poo.exo1;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;


public class LectureEcritureVehicule {

        
        private static ArrayList<Vehicule> vehiculesecrit; 
        private static ArrayList<Vehicule> vehiculeslu; 
        
        private static ArrayList<Vendeur> vendeursecrit; 
        
        private static ArrayList<Vehicule> readVehicules(Scanner in) {
                vehiculeslu = new ArrayList<Vehicule>();
                
                 
                 while (in.hasNextLine()) 
                {
                        String line =in.nextLine ();
                        String [ ] tokens = line.split ( "\\|" ) ;
                        int id = Integer.parseInt ( tokens [ 0 ] ) ;
                        String marque = tokens [ 1 ];
                        String modele = tokens [ 2 ] ;
                        String date = tokens [ 3 ] ;
                        double kilometrage = Double.parseDouble ( tokens [ 4 ] ) ;
                        double prix = Double.parseDouble ( tokens [ 5 ] ) ;
                        int nbExemplaire = Integer.parseInt ( tokens [ 6 ] ) ;
                        Vehicule vehicule = new Vehicule(id,marque,modele,date,kilometrage,prix ,nbExemplaire);
                        vehiculeslu.add(vehicule);
                }
                return vehiculeslu;
        }
        
        private static void writeVehicules(ArrayList<Vehicule> vehicules, PrintWriter out)
        {
                        for (Vehicule vehicule :  vehicules) 
                        {
                                vehicule.writeData(out);
                                
                        }
                        out.close();            
        }
        
        private static void writeVendeur(ArrayList<Vendeur> vendeurs, PrintWriter out)
        {
                        for (Vendeur vendeur :  vendeurs) 
                        {
                                vendeur.writeData(out);
                                
                        }
                        out.close();            
        }
                

        public static void main(String[] args) throws FileNotFoundException {
                
                vehiculesecrit = new ArrayList<Vehicule>();             
                vendeursecrit = new ArrayList<Vendeur>();
                
                
                Vehicule vehicule1 = new Vehicule(234,"Ford","Fiesta","2004",10000,5345,12);
                Vehicule vehicule2 = new Vehicule(456,"Renault","Clio","2001",15000,2300,5);
                vehiculesecrit.add(vehicule1);
                vehiculesecrit.add(vehicule2);
                        PrintWriter out;
                
                        try {
                                out = new PrintWriter(new FileWriter("vehiculesave.txt",true));
                                writeVehicules(vehiculesecrit, out);
                                
                                
                                Scanner in;
                                in = new Scanner(new File("vehiculesave.txt"));
                                vehiculeslu=readVehicules(in);
                                
                                for (Vehicule vehicule :  vehiculeslu) 
                                {
                                        System.out.println(vehicule.toString());
                                        
                                }
                        } catch (IOException e1) {
                                e1.printStackTrace();
                        }
                
                        
                        Vendeur vendeur = new Vendeur("Eric");
                        vendeur.addVehiculePossede(vehiculeslu.get(0));
                        vendeur.addVehiculePossede(vehiculeslu.get(0));
                        vendeur.addVehiculePossede(vehiculeslu.get(1));
                        vendeur.addVehiculePossede(vehiculeslu.get(0));
                        Vendeur vendeur2 = new Vendeur("Sophie");
                        vendeur2.addVehiculePossede(vehiculeslu.get(0));
                        vendeur2.addVehiculePossede(vehiculeslu.get(1));
                        vendeur2.addVehiculePossede(vehiculeslu.get(1));
                        vendeursecrit.add(vendeur);
                        vendeursecrit.add(vendeur2);
                        
                        
                        PrintWriter out2;
                        try {
                                out2 = new PrintWriter(new FileWriter("vendeursave.txt",true));
                                writeVendeur(vendeursecrit, out2);
                                
                                for (Vendeur vendeur1 :  vendeursecrit) 
                                {
                                        System.out.println(vendeur1.toString());
                                }
                                
                                out2.close();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                
        }

}



